package com.haoshui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoshui.constants.SystemConstants;
import com.haoshui.domain.ResponseResult;
import com.haoshui.domain.entity.Link;
import com.haoshui.domain.vo.LinkVo;
import com.haoshui.mapper.LinkMapper;
import com.haoshui.service.LinkService;
import com.haoshui.utils.BeanCopyUitls;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

@Service("LinkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {
    @Override
    public ResponseResult getAllLink() {
        //查找审核通过的 link
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL);
        List<Link> links = list(queryWrapper);

        //封装Vo
        List<LinkVo> copyBeanList = BeanCopyUitls.copyBeanList(links, LinkVo.class);

        return ResponseResult.okResult(copyBeanList);
    }

    @Override
    public Page<Link> pageLink(Integer pageNum, Integer pageSize, String name, String status) {
        //设置模糊查询的条件
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(status) && StringUtils.hasText(status), Link::getStatus, status);
        queryWrapper.like(Objects.nonNull(name) && StringUtils.hasText(name), Link::getName, name);

        //设置页
        Page<Link> page = new Page<>(pageNum, pageSize);

        //查询
        page(page, queryWrapper);

        return page;
    }
}
